package com.blb.service.impl;

import com.blb.entity.IoWarehouse;
import com.blb.mapper.IoWarehouseMapper;
import com.blb.service.IIoWarehouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blb.utils.ShiroUtils;
import com.blb.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 出入库表 服务实现类
 * </p>
 *
 * @author xiaoxiong
 * @since 2020-05-20
 */
@Service
public class IoWarehouseServiceImpl extends ServiceImpl<IoWarehouseMapper, IoWarehouse> implements IIoWarehouseService {

    @Resource
    private IoWarehouseMapper iowarehouseMapper;

    @Override
    public List<Map> areaName(String name) {
        return iowarehouseMapper.areaName(name);
    }

    @Override
    public List<Map> shelfNo(String warehouseName,String areaName) {
        Map<String,Object> map=new HashMap<>();
        map.put("warehouseName",warehouseName);
        map.put("areaName",areaName);
        return iowarehouseMapper.shelfNo(map);
    }

    @Override
    public List<Map> spaceNo(String warehouseName, String areaName, String shelfNo) {
        Map<String,Object> map=new HashMap<>();
        map.put("warehouseName",warehouseName);
        map.put("areaName",areaName);
        map.put("shelfNo",shelfNo);
        return iowarehouseMapper.spaceNo(map);
    }

    @Override
    public List<Map> storageAll() {
        String company = ShiroUtils.getCompany();
        return iowarehouseMapper.storageAll(company);
    }

    @Override
    public List<Map> handoverAll() {
        String company = ShiroUtils.getCompany();
        return iowarehouseMapper.handoverAll(company);
    }

    @Override
    @Transactional
    public void storageAdd(Map map) {
        //公司ID
        String company = ShiroUtils.getCompany();
        //分离出，入库的商品信息
        String pid =(String) map.get("pid");
        String ck =(String) map.get("ck");
        String hq =(String) map.get("hq");
        String hj =(String) map.get("hj");
        String hw =(String) map.get("hw");
        Map<String,Object> map1=new HashMap<>();
        String id = UUIDUtils.getId();
        map1.put("id",id);
        map1.put("space",id);
        map1.put("pid",pid); //商品id
        map1.put("ck",ck);
        map1.put("hq",hq);
        map1.put("hj",hj);
        map1.put("hw",hw);
        //调用商品入库操作
        iowarehouseMapper.spaceUpdate(map1);


        //订单ID 用于查找该订单并修改入库状态
        String oid =(String) map.get("oid"); //订单ID
        //调用修改订单状态操作
        iowarehouseMapper.ordUpdate(oid);


        //分离出，记录入库的信息
        String name =(String) map.get("name");
        String ioNo =(String) map.get("ioNo"); //入库单号
        String ioType =(String) map.get("ioType");//入库类型
        String batch =(String) map.get("batch");// 批次号
        //LocalDateTime ioTime =(LocalDateTime) map.get("ioTime"); //入库时间
        IoWarehouse ioWarehouse=new IoWarehouse();
        ioWarehouse.setId(UUIDUtils.getId());
        ioWarehouse.setPName(name);
        ioWarehouse.setIoNo(ioNo);
        ioWarehouse.setIoType(ioType);
        ioWarehouse.setBatch(batch);
        ioWarehouse.setPNum("1");
        ioWarehouse.setWName(ck);
        ioWarehouse.setCompanyId(company);
        //调用添加入库记录操作
        iowarehouseMapper.storAdd(ioWarehouse);
    }

    @Override
    public List<Map> outAll() {
        String company = ShiroUtils.getCompany();
        return iowarehouseMapper.outAll(company);
    }

    @Override
    public void outAdd(Map map) {
        //公司ID
        String company = ShiroUtils.getCompany();

        //订单ID 用于查找该订单并修改出库状态
        String oid =(String) map.get("oid"); //订单ID
        iowarehouseMapper.outUpdate(oid);

        //清除商品信息
        String pid= (String) map.get("pid");
        iowarehouseMapper.delPname(pid);

        //添加出库记录
        String id=UUIDUtils.getId();
        map.put("id",id);
        map.put("ioNo",id);
        map.put("ioType","销售订单出库");
        map.put("batch",id);
        map.put("pNum","1");
        map.put("company",company);
        iowarehouseMapper.outAdd(map);
    }

    @Override
    public List<IoWarehouse> warehouseAll(Map map) {
        //公司ID
        String company = ShiroUtils.getCompany();
        map.put("company",company);
        return iowarehouseMapper.warehouseAll(map);
    }

    @Override
    public int count(Map map) {
        //公司ID
        String company = ShiroUtils.getCompany();
        map.put("company",company);
        return iowarehouseMapper.count(map);
    }
}
